﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace WeiBOAPI.Common
{
    public class CommonTools
    {
        #region 打开IE
        /// <summary>
        /// 打开IE
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool OpenIE(string url)
        {
            try
            {
                System.Diagnostics.Process.Start(url);
                return true;
            }
            catch (System.ComponentModel.Win32Exception noBrowser)
            {
                Console.WriteLine(noBrowser.Message);
                return false;
            }
            catch (System.Exception other)
            {
                Console.WriteLine(other.Message);
                return false;
            }
        } 
        #endregion

        #region Utc时间转本地时间
        /// <summary>
        /// Utc时间转本地时间
        /// </summary>
        /// <param name="strValue">原格式：Wed Nov 17 15:07:48 +0800 2010</param>
        /// <returns></returns>
        public static string SplitDateTime(string strValue)
        {
            if (!string.IsNullOrEmpty(strValue))
            {
                //原格式：Wed Nov 17 15:07:48 +0800 2010
                string[] str = strValue.Split(' ');
                //转格式：Wed Nov 17 2010 15:07:48
                return str[0] + " " + str[1] + " " + str[2] + " " + str[5] + " " + str[3];
            }
            else
            {
                return "";
            }
        } 
        #endregion

        #region 反序列化XML文件到T Object
        public static T ReadXml<T>(string xmlFile)
        {
            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(T));
                XmlReader xmlReader = XmlReader.Create(xmlFile);
                T ps = (T)ser.Deserialize(xmlReader);
                xmlReader.Close();
                return ps;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public static T ReadXmlText<T>(string xmlContent)
        {
            try
            {
                xmlContent = xmlContent.Replace("\r\n", "");
                XmlSerializer ser = new XmlSerializer(typeof(T));
                XmlReader xmlReader = XmlReader.Create(new StringReader(xmlContent));
                T ps = (T)ser.Deserialize(xmlReader);
                xmlReader.Close();
                return ps;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 序列化Object到文件到XML文件
        public static void WriteXml<T>(T xmlObj, string outFileName)
        {
            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(T));
                StringBuilder sb = new StringBuilder();
                XmlWriter xmlWriter = XmlWriter.Create(outFileName);
                ser.Serialize(xmlWriter, xmlObj);
                xmlWriter.Close();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
